//
//  RootTabbarViewController.m
//  YunGou
//
//  Created by macbook on 16/5/16.
//  Copyright © 2016年 macbook. All rights reserved.
//

#import "RootTabbarViewController.h"
#import "RootNavViewController.h"

#import "HomeViewController.h"
#import "CategoryViewController.h"
#import "LatestViewController.h"
#import "ShoppingCartViewController.h"
#import "MineViewController.h"

#import "LoginViewController.h"

@interface RootTabbarViewController ()<UITabBarControllerDelegate>
@property (nonatomic,assign) NSInteger selectNum;
@property (nonatomic,strong) NSArray *VCArray;
@end

@implementation RootTabbarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupAllChildViewController];
    self.tabBar.tintColor = ThemeColor;
    self.tabBar.backgroundColor = [UIColor whiteColor];
    self.delegate = self;
}

/**为tabbar添加按钮*/
- (void)setupAllChildViewController
{
    HomeViewController *homeVC = SB(@"Home", @"HomeVC");
    [self setupChildViewController:homeVC
                             title:@"首页"
                         imageName:@"shouye_sy_hui"
                 selectedImageName:@"shouye_sy_cheng"];
    
    CategoryViewController *categoryVC = SB(@"Category", @"CategoryVC");
    [self setupChildViewController:categoryVC
                             title:@"分类"
                         imageName:@"shouye_fl_hui"
                 selectedImageName:@"shouye_fl_cheng"];
    
    LatestViewController *latestVC = SB(@"Latest", @"LatestVC");
    [self setupChildViewController:latestVC
                             title:@"最新揭晓"
                         imageName:@"shouye_zxjx_hui"
                 selectedImageName:@"shouye_zxjx_cheng"];
    
    ShoppingCartViewController *shopCartVC = SB(@"ShoppingCart", @"ShopCartVC");
    [self setupChildViewController:shopCartVC title:@"购物车" imageName:@"shouye_gwc_hui" selectedImageName:@"shouye_gwc_cheng"];
    
    MineViewController *mineVC = SB(@"Mine", @"MineVC");
    [self setupChildViewController:mineVC
                             title:@"我的"
                         imageName:@"shouye_wd_hui"
                 selectedImageName:@"shouye_wd_cheng"];
    
    _VCArray = @[homeVC,categoryVC,latestVC,shopCartVC,mineVC];
}

/**
 *  初始化一个子控制器
 *
 *  @param childVc           自控制器
 *  @param title             控制器标题
 *  @param imageName         图片
 *  @param selectedImageName 选中后图片
 */
- (void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName  selectedImageName:(NSString *)selectedImageName
{
    childVc.tabBarItem.title = title;
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    //设置选中后的图片并且取消渲染效果
    if(IOS7){
        childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }else{
        childVc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageName] ;
    }
    //使用nav包装控制器
    RootNavViewController *nav = [[RootNavViewController alloc] initWithRootViewController:childVc];
    [self addChildViewController:nav];
}

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    if (tabBarController.selectedIndex == 3) {
        if (!IsLogin) {
            tabBarController.selectedIndex = self.selectedIndex;
            LoginViewController *loginVC = SB(@"Login", @"LoginVC");
            loginVC.isPresent = YES;
            RootNavViewController *nav = [[RootNavViewController alloc] initWithRootViewController:loginVC];
            tabBarController.selectedIndex = _selectNum;
            [_VCArray[_selectNum] presentViewController:nav animated:YES completion:nil];
        }
    }else{
        _selectNum = tabBarController.selectedIndex;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
